//
//  PreviewView.m
//  NewsMaker Touch
//
//  Created by Reisen on 9/30/14.
//  Copyright (c) 2014 Reisen. All rights reserved.
//

#import "PreviewView.h"
#import "Segment.h"
#import "InterviewSegment.h"
#import "TalkDefine.h"

@implementation PreviewView

-(void)setSegments:(NSMutableArray *)segments
{
    backward = NO;
    [self.layoutView setClipsToBounds:YES];
    
    _segments = [NSMutableArray array];
    for(Segment* seg in segments)
    {
        if([seg canPreview])
        {
            [_segments addObject:seg];
        }
    }
    
    isClosing = NO;
    
    [self.playPauseButton setTitle:@"PLAY" forState:UIControlStateNormal];
    self.playPauseButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [self loadSegment:[_segments firstObject]];
}

-(void)loadSegment:(Segment*)seg
{
    currentSegment = seg;
    seg.currentPreviewLayer = self;
    
    [seg initializePreview];
}

-(void)playNextSegment
{
    if(isClosing)
    {
        [self removeFromSuperview];
        return;
    }
    
    if(!backward)
    {
        if(currentSegment == [self.segments lastObject])
        {
            [self loadSegment:[self.segments objectAtIndex:0]];
        
            [self.delegate playbackCompleted:self];
        }
    
        else
        {
            NSInteger index = [self.segments indexOfObject:currentSegment] + 1;
        
            TLDebugS(@"Load segment %zd", index);
            [self loadSegment:[self.segments objectAtIndex:index]];
        
            [currentSegment play];
        }
    }
    
    else
    {
        if(currentSegment == [self.segments firstObject])
        {
            //   [self removeFromSuperview];
            [self loadSegment:[self.segments objectAtIndex:0]];
        }
        
        else
        {
            NSInteger index = [self.segments indexOfObject:currentSegment] - 1;
            
            TLDebugS(@"Load segment %zd", index);
            [self loadSegment:[self.segments objectAtIndex:index]];
            
            if([currentSegment isKindOfClass:[InterviewSegment class]])
                [(InterviewSegment*)currentSegment goToLastQuestion];
            
            else
                [currentSegment play];
        }
    }
}

-(IBAction)close:(id)sender
{
    isClosing = YES;
    
    [currentSegment stop];
}


-(void) play
{
    [currentSegment play];
}

-(void) stop
{
    [currentSegment stop];
}

-(void) pause
{
    [currentSegment pause];
}

-(void)onSwipedLeft:(id)sender
{
    if([currentSegment isKindOfClass:[InterviewSegment class]])
    {
        [(InterviewSegment*)currentSegment swipeLeft];
    }
    
    else {
        [currentSegment stop];
    }
}

-(void)onSwipedRight:(id)sender
{
    if(currentSegment == [self.segments firstObject])
        return;

    backward = YES;
    if([currentSegment isKindOfClass:[InterviewSegment class]])
    {
        [(InterviewSegment*)currentSegment swipeRight];
    }
    
    else {
        [currentSegment stop];
    }
    
    backward = NO;
}
@end